package it.appcatalogobase;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
 
public class DoveSiamoFragment extends Fragment {
	DoveSiamoDataSource dataDS;
	SharedPreferences prefs;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        prefs = getActivity().getSharedPreferences("SyncPref", Context.MODE_PRIVATE);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_dovesiamo, container, false);

       
        return rootView;
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState){
    	super.onActivityCreated(savedInstanceState); 
    	 if(!prefs.getBoolean("FirstSync", true)){
         	dataDS = new DoveSiamoDataSource(getActivity());
         
 	        List<DoveSiamo> lista = dataDS.getAllDoveSiamo();
 	        try {
 				String ind = URLDecoder.decode(Html.fromHtml(lista.get(0).getIndirizzo()).toString(), "ISO-8859-1");
 				TextView txt = (TextView) getActivity().findViewById(R.id.textViewInd);
 				if(ind!=null && !ind.equals(""))
 					txt.setText("INDIRIZZO: "+ind);
 				else
 					txt.setVisibility(View.GONE);
 				String cell = URLDecoder.decode(Html.fromHtml(lista.get(0).getCell()).toString(), "ISO-8859-1");
 				TextView txtC = (TextView) getActivity().findViewById(R.id.textViewCell);
 				if(cell!=null && !cell.equals(""))
 					txtC.setText(cell);
 				else
 					txtC.setVisibility(View.GONE);
 				String tel = URLDecoder.decode(Html.fromHtml(lista.get(0).getTel()).toString(), "ISO-8859-1");
 				TextView txtT = (TextView) getActivity().findViewById(R.id.textViewTel);
 				if(tel!=null && !tel.equals(""))
 					txtT.setText(tel);
 				else
 					txtC.setVisibility(View.GONE);
 				String mail = URLDecoder.decode(Html.fromHtml(lista.get(0).getMail()).toString(), "ISO-8859-1");
 				TextView txtM = (TextView) getActivity().findViewById(R.id.textViewMail);
 				if(mail!=null && !mail.equals(""))
 					txtM.setText(ind);
 				else
 					txtM.setVisibility(View.GONE);
 				String fax = URLDecoder.decode(Html.fromHtml(lista.get(0).getFax()).toString(), "ISO-8859-1");
 				TextView txtF = (TextView) getActivity().findViewById(R.id.textViewFax);
 				if(fax!=null && !fax.equals(""))
 					txtF.setText(fax);
 				else
 					txtF.setVisibility(View.GONE);
 	        } catch (UnsupportedEncodingException e) {
 				// TODO Auto-generated catch block
 				e.printStackTrace();
 			}
         }else{
         	TextView txt = (TextView) getActivity().findViewById(R.id.textViewInd);
         	txt.setText("E' necessario fare almeno una sincronizzazione");
         	TextView txtC = (TextView) getActivity().findViewById(R.id.textViewCell);
         	txtC.setVisibility(View.GONE);
         	TextView txtF = (TextView) getActivity().findViewById(R.id.textViewFax);
         	txtF.setVisibility(View.GONE);
         	TextView txtM = (TextView) getActivity().findViewById(R.id.textViewMail);
         	txtM.setVisibility(View.GONE);
         	TextView txtT = (TextView) getActivity().findViewById(R.id.textViewTel);
         	txtT.setVisibility(View.GONE);
         }
    }
}